//
//  ProtocolViewController.m
//  UniversalApp
//
//  Created by 奇林刘 on 2018/11/29.
//  Copyright © 2018 voidcat. All rights reserved.
//

#import "ProtocolViewController.h"
#import "AuthenticationViewController.h"
#import <WebKit/WebKit.h>

@interface ProtocolViewController () <WKNavigationDelegate>

@property (nonatomic, strong) WKWebView *webView;
@property (nonatomic, strong) UIButton *okButton;

@end

@implementation ProtocolViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = ASLocalizedString(@"主播协议");
    self.view.backgroundColor = CViewBgColor;
    [self setupUI];
}

- (void)setupUI {
    self.webView = [[WKWebView alloc] init];
    self.webView.navigationDelegate = self;
    [self.view addSubview:self.webView];
    
    self.okButton = [[UIButton alloc] init];
    [self.view addSubview:self.okButton];
    
    [self layoutUI];
    [self configUI];
}

- (void)layoutUI {
    [self.webView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.view.mas_top).offset(20);
        make.width.equalTo(@(KScreenWidth - 40));
        make.centerX.equalTo(self.view);
        make.bottom.equalTo(self.okButton.mas_top).offset(-20);
    }];
    
    [self.okButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(self.view.mas_bottom).offset(-20);
        make.width.equalTo(@(KScreenWidth - 40));
        make.height.equalTo(@(50));
        make.centerX.equalTo(self.view);
    }];
}

- (void)configUI {
    ViewBorderRadius(self.webView, 10, 1, CLineColor);
    ViewBorderRadius(self.okButton, 10, 1, CLineColor);
    [self.okButton addTarget:self action:@selector(goVerify) forControlEvents:UIControlEventTouchUpInside];
    [self.okButton setBackgroundColor:UIColorWhite];
    [self.okButton setTitle:@"我已阅读并同意" forState:UIControlStateNormal];
    [self.okButton setTitleColor:UIColorBlack forState:UIControlStateNormal];
    
    [self loadProtocol];
}

- (void)goVerify {
    AuthenticationViewController *vc = [AuthenticationViewController new];
    vc.title = ASLocalizedString(IsCurrentUserMale ? @"实名认证" : @"成为主播");
    [self.navigationController pushViewController:vc animated:NO];
}

- (void)loadProtocol {
        NSString *url = [[CYURLUtils sharedCYURLUtils] makeURLWithC:@"user_api" A:@"get_protocol"];
    
        NSMutableDictionary *param = [NSMutableDictionary dictionary];
    
        [CYNET POSTv2:url parameters:param responseCache:^(id responseObject) {
    
        } success:^(id responseObject) {
            NSString *protocolSource = responseObject[@"data"];
            [self.webView loadHTMLString:protocolSource baseURL:nil];
        } failure:^(NSString *error) {
            [MBProgressHUD showTopTipMessage:error];
        } hasCache:NO];
}

- (void)webView:(WKWebView *)webView didFinishNavigation:(WKNavigation *)navigation {
    [webView evaluateJavaScript:@"document.documentElement.style.webkitUserSelect='none';" completionHandler:nil];
    [webView evaluateJavaScript:@"document.activeElement.blur();" completionHandler:nil];
    [webView evaluateJavaScript:@"document.getElementsByTagName('body')[0].style.webkitTextSizeAdjust= '300%'" completionHandler:nil];
}



@end
